package com.zw.syncopenapi.common;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * InventoryScheduler -
 * Created by Jack on 2025/9/29.
 * Description:
 */
@Component
public class InventoryScheduler {

    @Value("${jsonFilePath}")
    private String jsonFilePath;

    @Value("${oaidFilePath}")
    private String oaidFilePath;

    @Autowired
    private  JsonFileReader jsonFileReader;
    @Autowired
    private ExcelFileUtils excelFileUtils;

    @Autowired
    private VivoApiUtil vivoApiUtil;

    private final ConcurrentHashMap<String, String> cache = new ConcurrentHashMap<>();

    @Scheduled(cron = "0 0 22 * * ?")
    public void resetModify() throws Exception {
        String s1 = cache.get("lastMD5");
        System.out.println(cache);
        File latestModifiedFile = excelFileUtils.getLatestModifiedExcelFile(oaidFilePath);
        String s = excelFileUtils.calculateFileMD5(latestModifiedFile);
        if (Objects.equals(s1, s)){
            System.out.println("未找到新文件：" + LocalDateTime.now());
        }else {
            System.out.println("执行任务：" + LocalDateTime.now());
            //处理excel中的oaid为txt


            List<String> columnBAsStringArray = excelFileUtils.getColumnBAsStringArray(latestModifiedFile);

            String filePath = excelFileUtils.convertListToTextFile(columnBAsStringArray);

            System.out.println(filePath);
            JSONArray objects = jsonFileReader.readJsonFromAbsolutePath(jsonFilePath);
            System.out.println(objects);

            for(int i=0;i<objects.size();i++){
                JSONObject jsonObject = objects.getJSONObject(i);
                String token = jsonObject.getString("token");
                JSONArray jsonArray = jsonObject.getJSONArray("data");
                for (int j=0;j<jsonArray.size();j++){
                    JSONObject jsonObject1 = jsonArray.getJSONObject(j);
                    String uuid = jsonObject1.getString("uuid");
                    JSONObject postBody = jsonObject1.getJSONObject("postBody");
                    //先上传 人群文件
                    String response = vivoApiUtil.uploadDmpTagFile(filePath,uuid,token);
                    JSONObject jsonObject2 = JSONObject.parseObject(response);
                    Integer code = jsonObject2.getInteger("code");
                    if (code == 0){
                        String fileId = jsonObject2.getString("data");
                        postBody.put("fileIds", fileId);
                        String post = vivoApiUtil.post("/v1/ad/dmpTag/modify", uuid, token, postBody.toJSONString());
                        System.out.println(post);
                    }else {
                        System.out.println("文件上传失败："+response);
                    }

                    System.out.println(response);
                }
            }







            System.out.println("成功："+ latestModifiedFile.getName());
            cache.put("lastMD5",s );
        }
    }


}
